package com.example.springbootdemo.listen;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.springbootdemo.mall.service.OrderProductItemService;
import com.example.springbootdemo.scene.entity.ext.redismq.MarkScene;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class Receiver {

    @Autowired
    private OrderProductItemService orderProductItemService;



    /**
     * 接收系统消息，开启异步监听
     *
     * @param message redis中的序列化串
     */

    @Async
    public void markScene(String message) {
        log.info("{}", message);
        message = getTrueString(message);
        MarkScene markScene = JSON.parseObject(message, MarkScene.class);
        log.info("redis消息发布订阅转换的对象是{}",markScene.toString());
        //TODO 拿到对象做业务

    }

    /**
     * 获取到正确的Json 字符串 "" {"/a":1}""  变为 "{"a":1}"
     *
     * @param message
     */
    private String getTrueString(String message) {
        message = StringEscapeUtils.unescapeJavaScript(message);
        return message.substring(1, message.length() - 1);
    }

}
